package com.future.bantang.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.future.bantang.R;
import com.future.bantang.constants.Constants;
import com.future.bantang.eventmas.BaseEventMsg;
import com.future.bantang.fragment.RecommendHotFragment;
import com.future.bantang.view.PagerSlidingTabStrip;
import com.future.bantang.view.ViewPagerUp;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * create by liufujie 2016-01-14
 * 热门商品详细信息（手机控，杯子控等）
 */
@ContentView(R.layout.activity_recommend_detail)
public class RecommendDetailActivity extends BaseActivity {

    private static final String TAG = "RecommendDetailActivity";

    //控件列表
    @ViewInject(R.id.tv_recommend_hot_title)
    private TextView tvRecommendTitle;

    //viewpager指示器
    @ViewInject(R.id.psts_recommend_hot)
    private PagerSlidingTabStrip pstsRecommenedHot;

    @ViewInject(R.id.vp_recommend_hot)
    private ViewPagerUp vpRecommenedHot;

    //传入的数据id
    private String element_id;

    //传入副本数据id
    private String extend_id;

    //viewpager适配器
    private MypagerAdapter pagerAdapter;

    //判断是否有两个标签
    private boolean isDoubleTag;

    /**
     * 初始化控件
     */
    @Override
    protected void init() {
        super.init();

        Intent intent = getIntent();

        tvRecommendTitle.setText(intent.getStringExtra("element_title"));

        element_id = intent.getStringExtra("element_id");

        extend_id = intent.getStringExtra("extend");

        isDoubleTag = !element_id.equals(extend_id);

        pagerAdapter = new MypagerAdapter(getSupportFragmentManager());

        vpRecommenedHot.setAdapter(pagerAdapter);

        pstsRecommenedHot.setViewPager(vpRecommenedHot);

        //如果没有多个选项，则隐藏viewpager指示器
        if (!isDoubleTag) {
            pstsRecommenedHot.setVisibility(View.GONE);
        }

        vpRecommenedHot.setIsCanSrcoll(isDoubleTag);
    }

    /**
     * 加载数据到控件
     */
    @Override
    protected void loadDatas() {

    }

    /**
     * 返回按钮
     *
     * @param view
     */
    @OnClick(R.id.iv_recommed_hot_back)
    public void back(View view) {
        this.finish();
    }

    /**
     * viewpager适配器
     */
    class MypagerAdapter extends FragmentPagerAdapter {

        public MypagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Constants.Tags[position];
        }

        @Override
        public Fragment getItem(int position) {
            if (isDoubleTag) {
                return RecommendHotFragment.newInstance(extend_id, isDoubleTag, position);
            } else {
                return RecommendHotFragment.newInstance(element_id, isDoubleTag, position);
            }
        }

        /**
         * 是否有多个页面
         *
         * @return
         */
        @Override
        public int getCount() {
            if (isDoubleTag) {
                return Constants.Tags.length;
            }
            return 1;
        }
    }
}
